﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace REVISIONMAX30
{
    public partial class frmSQLItem : Form
    {
        public frmSQLItem()
        {
            InitializeComponent();
        }

        public UIHandler uiHandler { get; set; }
        public DataObjects.SQLItem sqlItem { get; set; }
        public DataObjects.Group group { get; set; }

        public void View(UIHandler pUIHandler, DataObjects.SQLItem pChildObject, DataObjects.Group pParentObject)
        {
            this.uiHandler = pUIHandler;
            this.sqlItem = pChildObject;
            this.group = pParentObject;
            this.UpdateViewer();
            base.ShowDialog();
        }

        public Guid DataObjectGUID
        {
            get { return this.sqlItem.DataObjectGUID; }
        }

        private void UpdateObject()
        {
            this.sqlItem.ExecutionOrderIndex = this.txtIndex.Text.IntValueOrZero();
            this.sqlItem.Revision = this.txtRevision.Text.IntValueOrZero();
            this.sqlItem.SourceType = this.cboSourceType.SelectedValue.StringValueOrEmpty();
            if (this.sqlItem.SourceType == "SQLFILE")
            {
                this.sqlItem.Source = this.txtFileName.Text;
            }
            else if (this.sqlItem.SourceType == "COMMANDTEXT")
            {
                this.sqlItem.CommandText = this.txtCommandText.Text;
            }

            if (this.cboConnectionStrings.SelectedValue != null)
            {
                this.sqlItem.SelectedConnectionStringGUID = (Guid)this.cboConnectionStrings.SelectedValue;
            }
            else
            {
                this.sqlItem.SelectedConnectionStringGUID = null;
            }
        }

        private void UpdateViewer()
        {
            this.btnExpire.Text = !this.sqlItem.Expired.HasValue ? "Expire" : "Un-Expire";
            this.txtExecuted.Text = this.sqlItem.Executed.StringValueOrEmpty();
            this.txtExpired.Text = this.sqlItem.Expired.StringValueOrEmpty();
            this.txtIndex.Text = this.sqlItem.ExecutionOrderIndex.ToString();
            this.txtRevision.Text = this.sqlItem.Revision.StringValueOrEmpty();
            if (this.sqlItem.SourceType == "SQLFILE")
            {
                this.txtFileName.Text = this.sqlItem.Source;
            }
            else if (this.sqlItem.SourceType == "COMMANDTEXT")
            {
                this.txtCommandText.Text = this.sqlItem.CommandText;
            }

            this.cboSourceType.DataSource = Types.SourceTypeList;
            this.cboSourceType.DisplayMember = "Value";
            this.cboSourceType.ValueMember = "Key";
            this.cboSourceType.SelectedValue = this.sqlItem.SourceType;

            this.cboConnectionStrings.DataSource = this.group.ConnectionStringList.Select(t => new { Key = t.DataObjectGUID, Value = t.Name });
            this.cboConnectionStrings.DisplayMember = "Value";
            this.cboConnectionStrings.ValueMember = "Key";
            this.cboConnectionStrings.SelectedValue = this.sqlItem.SelectedConnectionStringGUID;
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            this.Save(true);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Save(false);
        }

        public void Save(bool pApply)
        {

            UpdateObject();

            if (!group.SQLItemList.Exists(t => t.DataObjectGUID == this.DataObjectGUID))
            {
                group.SQLItemList.Add(this.sqlItem);
            }

            if (!pApply)
            {
                this.Close();
            }

        }

        private void btnExpire_Click(object sender, EventArgs e)
        {
            if (this.sqlItem.Expired != null)
                this.sqlItem.Expired = DateTime.Now;
            else
                this.sqlItem.Expired = null;

            this.btnExpire.Text = !this.sqlItem.Expired.HasValue ? "Expire" : "Un-Expire";
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);

            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtFileName.Text = this.openFileDialog1.FileName;
            }
        }

        private void txtFileName_TextChanged(object sender, EventArgs e)
        {
            txtFileName.ForeColor = System.IO.File.Exists(txtFileName.Text) ? Color.Black : Color.Red;
        }

    }
}
